<?php

namespace App\Http\Controllers\Api\V1;

use App\Org\Developer\JsonReturnInfo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * @author 陈玉琦
 * 测试控制器
 */
class TestController extends Controller
{
//	public function __construct(Request $request)
//	{
//		$this->check($request);
//	}

    public function download()
    {
        echo 245345;
        die;
    }

    public function index(Request $request)
    {
        echo public_path();
        exit;


        $result = \Illuminate\Support\Facades\Cache::put('foo', 'bar', 3000);// store('file')->
        //var_dump($result);;die;
        var_dump(\Illuminate\Support\Facades\Cache::get('foo'));
        die;

        echo \App\Org\Developer\Helper::makeQrcodeImage('http://www.jb51.net', 10);
        return;
        // $qrcodeImagePath = './qrcodes/5/'. time() . '.png';
        // \App\Org\Developer\QRcode::png('http://www.jb51.net', $qrcodeImagePath);
        // return ;
        // $qrcodeImage = 
        // return JsonReturnInfo::infoSuccess('调查投票创建成功.', ['qrcode_image' => $qrcodeImage]);


        // return ['code' => 200];

        // dd(['code' => 200]);

        $result = \App\Models\Test::insert([
            ['title' => 'batch title 007', 'summary' => ''],
            ['title' => 'batch title 008', 'summary' => 'batch summary 008'],
        ]);
        var_dump($result);
        exit;

        $result = \App\Models\VoteResult::getParticipartIds();
        print_r($result);
        exit;

        $result = \App\Models\VoteOption::getOptionIdsByActivityId(2)->toArray();
        print_r($result);
        exit;


        $result = \App\Models\Test::select(['id', 'title'])->find(5);
        return $result;


        $result = \App\Models\Test::where([['id', '>', 5], ['title', 'like', '%batch%']])->get();
        return $result;


        /*
                return \App\Models\QrcodeImage::getQrcodeImagesByActivityId(1);
                return \App\Models\QrcodeImage::getQrcodeImagesByUserId(1);
                exit;


                $result = \App\Models\Test::where([['id', '<', 8]])->delete();
                    ($result);
                exit;

                print_r(\App\Models\Test::select(['id', 'title'])->find(5)->all());
                exit;

                \App\Models\Test::insert([
                    ['title' => 'batch title 001', 'summary' => 'batch summary 001'],
                    ['title' => 'batch title 002', 'summary' => 'batch summary 002'],
                    ['title' => 'batch title 003', 'summary' => 'batch summary 003'],
                ]);
                exit;

                return \App\Models\Vote::getVotesByUserId(1);
                exit;

                return Test::getData();
                exit;

                // echo $request->input('aaa');
                echo 123;
                exit;
                return response()->json([
                    'code' => '500',
                    'text' => 'server error ............'
                ]);
                return [
                    'code' => '500',
                    'text' => 'server error ...'
                ];
                    ($request->server('DOCUMENT_ROOT'));
                echo '<br/>';
                    (config('api.fileSavePath.activity_image_path'));
                exit;
        */
    }

    public function test()
    {
        // 增

        // $inserted = \Illuminate\Support\Facades\DB::insert('insert into thearte_tests (title) values (?)', ['Dayle']);
        //     ($inserted);// bool(true)

        // $result = \Illuminate\Support\Facades\DB::table('tests')->insert(['title' => 'title 001']);
        //     ($result);// bool(true)

        // $result = Test::create(['title' => 'title 002']);
        //     ($result);// object(App\Models\Test)

        // $testModel = new Test();
        // $testModel->title = 'title 003';
        // $result = $testModel->save();
        //     ($result);// bool(true)


        // 改

        // $affected = DB::update('update users set votes = 100 where name = ?', ['John']);
        // 返回受更新语句影响的行数

        // $result = \Illuminate\Support\Facades\DB::table('tests')->where(['id' => 1])->update(['summary' => 'hello summary 001']);
        //     ($result);// int(1)

        // $result = Test::where(['id' => 2])->update(['summary' => 'hello summary 002']);
        //     ($result);// int(1)

        // $testModel = Test::where(['id' => 3])->first();
        // $testModel->summary = 'hello summary 003';
        // $result = $testModel->save();
        //     ($result);// bool(true)


        // 删

        // $result = \Illuminate\Support\Facades\DB::delete('delete from thearte_tests where id = :id', ['id' => 1]);
        //     ($result);
        // 返回被删除的行数，当没有符合条件的数据要删除时返回 int(0)。

        // $result = \Illuminate\Support\Facades\DB::table('tests')->where(['id' => 2])->delete();
        //     ($result);// int(1)

        // $testModel = Test::where(['id' => 4])->first();
        // $result = $testModel->delete();
        //     ($result);// bool(true)

        // 如果要删除已经被软删除的数据，必须使用 onlyTrashed() 或者 withTrashed() 方法
        // $testModel = Test::withTrashed()->where(['id' => 4])->first();
        // $result = $testModel->forceDelete();
        //     ($result);// bool(true)

        // 根据主键删除
        // Test::destroy(1);
        // $result = Test::destroy([1]);
        //     ($result);// int(1)
        // Test::destroy(1, 2, 3);
        // $result = Test::forceDelete([4]);// 非静态方法 不能这样调用。
        //     ($result);报错：Non-static method App\\Models\\Test::forceDelete() should not be called statically, assuming $this from incompatible context


        // 查

        // 多个
        // $results = \Illuminate\Support\Facades\DB::select('select * from thearte_tests where id = :id', ['id' => 1]);
        //     ($results);
        /*
array(1) {
  [0]=>
  object(stdClass)#339 (7) {
    ["id"]=>
    int(1)
    ["title"]=>
    string(9) "title 001"
    ["summary"]=>
    string(17) "hello summary 001"
    ["content"]=>
    NULL
    ["created_at"]=>
    string(19) "2017-08-20 15:36:13"
    ["updated_at"]=>
    string(19) "2017-08-20 15:37:22"
    ["deleted_at"]=>
    NULL
  }
}
         */

        // $results = \Illuminate\Support\Facades\DB::select('select * from thearte_tests where id = :id', ['id' => 1]);
        //     ($results);
        // 这个查询不能像项目的 get() 那样连缀 all(); 不能 toArray(); 和 asArray();

        // 多个
        // $result = \Illuminate\Support\Facades\DB::table('tests')->select(['title', 'summary'])->get();
        //     ($result);
        /*
object(Illuminate\Support\Collection)#339 (1) {
  ["items":protected]=>
  array(3) {
    [0]=>
    object(stdClass)#340 (2) {
      ["title"]=>
      string(9) "title 001"
      ["summary"]=>
      string(17) "hello summary 001"
    }
    ...
  }
}
         */

        // 多个
        // $result = \Illuminate\Support\Facades\DB::table('tests')->select(['title', 'summary'])->get()->all();
        //     ($result);
        /*
array(3) {
  [0]=>
  object(stdClass)#340 (2) {
    ["title"]=>
    string(9) "title 001"
    ["summary"]=>
    string(17) "hello summary 001"
  }
  ...
}
         */

        // 一个
        // $testModel = Test::where(['id' => 3])->first();
        //     ($testModel);// object(App\Models\Test)

        // first() 连缀 一个 all() 后，结果又变成多个了（集合）
        // $testModel = $testModel = Test::where(['id' => 3])->first()->all();
        //     ($testModel);
        /*
object(Illuminate\Database\Eloquent\Collection)#341 (1) {
  ["items":protected]=>
  array(3) {
    [0]=>
    object(App\Models\Test)#346 (23) {
        ...
         */

        // first() 连缀 两个 all() 后，结果又变成多个了（模型类的对象数组）。
        // $testModel = $testModel = Test::where(['id' => 3])->first()->all()->all();
        //     ($testModel);
        /*
        array(3) {
            [0]=>
            object(App\Models\Test)#346 (23) {
                ...
        */

        // 一个（后面可以连缀 一个 或者 两个 all()，查看上面的 get() 方法）
        // $testModel = Test::find(3); 这里并不是 where 的写法，而是需要主键对应的值（一个或多个）。
        //     ($testModel);// object(App\Models\Test)

        // 多个（可以连缀 一个 all() 得到第一个模型类对象的数组）
        // $testModel = Test::find([1,2,3]);
        //     ($testModel);
        /*
        object(Illuminate\Database\Eloquent\Collection)#345 (1) {
          ["items":protected]=>
          array(3) {
            [0]=>
            object(App\Models\Test)#346 (23) {
            ...
        */
    }

    public function abc()
    {
        // 大屏幕图片（可能会有多个，因此，单独创建一个大屏幕图片表）
        if (!empty($screen_image_data = $request->input('screen_image', ''))) {
            // 保存图片，返回路径
            if ($temp_path = FileHandler::saveNodeFile($screen_image_data, config('api.fileSavePath.activity_image_path'))) {
                $createModel = ScreenImage::create([
                    'activity_id' => $activity_id,
                    'image_url' => $temp_path
                ]);
                if (!(isset($createModel) && $createModel->id)) {
                    return JsonReturnInfo::infoFailure('大屏幕图片信息写入失败.');
                }
            } else {
                return JsonReturnInfo::SAVE_FILE_ERROR;
            }
        }

        // 二维码图片（可能会有多个，因此，单独创建一个二维码图片表）
        if (!empty($qrcode_image_data = $request->input('qrcode_image', ''))) {
            // 保存图片，返回路径
            if ($temp_path = FileHandler::saveNodeFile($qrcode_image_data, config('api.fileSavePath.activity_image_path'))) {
                $createModel = QrcodeImage::create([
                    'activity_id' => $activity_id,
                    'image_url' => $temp_path
                ]);
                if (!(isset($createModel) && $createModel->id)) {
                    return JsonReturnInfo::infoFailure('二维码图片信息写入失败.');
                }
            } else {
                return JsonReturnInfo::SAVE_FILE_ERROR;
            }
        }
    }
}